package com.winterwar.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.winterwar.base.Event;
import com.winterwar.dao.EventDAO;

public class EventDAOHibernate extends HibernateDaoSupport implements EventDAO {

	private Log log = LogFactory.getLog(EventDAO.class);
	
	public Event get(Integer eventID) {
		return (Event) getHibernateTemplate().get(Event.class, eventID);
	}

	public List getAll() {
		return getHibernateTemplate().find("from Event order by eventName");
	}

	public void remove(Integer eventID) {
		Object event = getHibernateTemplate().load(Event.class, eventID);
		getHibernateTemplate().delete(event);
		getHibernateTemplate().flush();
	}

	public void save(Event event) {
		getHibernateTemplate().saveOrUpdate(event);
		getHibernateTemplate().flush();
		if(log.isDebugEnabled()){
			log.debug("Event ID set to: " + event.getEventID());
		}
	}

	public List getByConID(Integer conID, String status) {
		String clause = "from Event where conID = " + conID;
		if(status != null && !status.equals(""))
			clause = clause + " and status = " + status;
		return getHibernateTemplate().find(clause + " order by slotID, eventType, eventName");
	}

	public List getByTableSlot(Integer slotID, Integer tableID) {
		return getHibernateTemplate().find("from Event where slotID = " + slotID + " and tableID = " + tableID + " and status = 3");
	}

}
